//
//  LDChooseTimeView.m
//  指乎
//
//  Created by Dean on 2018/11/16.
//  Copyright © 2018 Dean. All rights reserved.
//
#import "LDBottomPopView.h"
#import "LDChooseTimeView.h"
#define DDBtnWidth RatioWidth(107)
@interface LDChooseTimeView ()<PGDatePickerDelegate>
@property (nonatomic, strong)UIView *ccvView;
@property (nonatomic, strong)UIButton *curentBtn;
@property (nonatomic, strong)NSString *startTime;
@property (nonatomic, strong)NSString *endTime;
@property (nonatomic, strong)UIView *contentView;
@property (nonatomic, strong)LDBottomPopView *popView;

@end
@implementation LDChooseTimeView
-(instancetype)init{
    if (self = [super initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, RatioHeight(189))]) {
        self.backgroundColor = [UIColor whiteColor];
        
        UIView *view =[[UIView alloc] initWithFrame:CGRectMake(0, 0 , SCREEN_WIDTH ,self.startButton.bottom + RatioHeight(40))];
        _ccvView = view;
        view.backgroundColor = [UIColor whiteColor];
        [self addSubview:view];
        
        self.curentBtn = self.startButton;
        [self addSubview:self.startButton];
        self.startButton.selected = YES;
        self.startButton.top = RatioHeight(12);
        self.startButton.left = RatioWidth(58);
        [self addSubview:self.endButton];
        self.endButton.top = RatioHeight(12);
        self.endButton.left = self.startButton.right + RatioWidth(46);
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(self.startButton.right, self.startButton.top, RatioWidth(46), self.startButton.height)];
        label.font = TGAutoFont(13);
        label.textColor = RGBA(51, 51, 51, 1);
        [self addSubview:label];
        label.textAlignment = NSTextAlignmentCenter;
        label.text = @"至" ;
        UIView *view1 =[[UIView alloc] initWithFrame:CGRectMake(RatioWidth(70), label.bottom + RatioHeight(20) , SCREEN_WIDTH -  RatioWidth(70*2) , RatioHeight(93))];
        view1.backgroundColor = [UIColor whiteColor];
        [self addSubview:view1];
        _contentView = view1;
        [view1 addSubview:self.datePicker1];
        self.datePicker1.frame = _contentView.bounds;
    }
    return self;
}
- (void)setIsBlueModel:(BOOL)isBlueModel {
    _isBlueModel = isBlueModel;
    self.startButton.blueModel =_isBlueModel;
    self.endButton.blueModel = _isBlueModel;
    if (_isBlueModel) {
        _datePicker1.textColorOfSelectedRow = GlobalBarTinColor;
        _datePicker1.lineBackgroundColor = GlobalBarTinColor;
    }
}

- (void)layoutSubviews {
    [super layoutSubviews];

}
- (void)sureAction:(id)sender{
    
    if (self.startTime && self.endTime) {
        if (![NSDate validateWithStartTime:self.startTime endTime:self.endTime]) {
            [TGProgressHUD showHUDTip:@"起始时间不能大于结束时间"];
            return;
        }
    }
    if (self.sureSelectTime) {
        self.sureSelectTime(self.startTime, self.endTime);
    }
    [_popView hide];
    
}
- (void)show {
    
    if (!_popView) {
        _popView = [[LDBottomPopView alloc] initWithAlertView:self];
        _popView.sureBtnEnable = NO;
        if (self.isBlueModel) {
            [_popView.rightBtn setTitleColor:GlobalBarTinColor forState:0];
        }
        [_popView addTagert:self selector:@selector(sureAction:)];
    }
//    __weak LDBottomPopView *temp = _popView;
//    [_popView setSureblock:^{
//        if (self.startTime && self.endTime) {
//            temp.sureBtnEnable = YES;
//        }else{
//            [LDProgressHUD showHUDTip:@"请选择有效期时间"];
//        }
//    }];
    [_popView show];
}

- (void)setCurrentDate:(NSDate *)currentDate {
    if (_currentDate !=nil ) {
        return;
    }
    _currentDate = currentDate;
    [self.datePicker1 setDate:currentDate animated:YES];

}
- (void)setMinDate:(NSDate *)minDate {
    _minDate = minDate;
    self.datePicker1.minimumDate = self.minDate;
}
- (PGDatePicker *)datePicker1{
    if (!_datePicker1) {
        _datePicker1 = [[PGDatePicker alloc]init];
        _datePicker1.autoSelected = YES;
        //        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
        //            dateFormatter.dateFormat = @"yyyy-MM-dd";
        //        _datePicker.minimumDate = [dateFormatter dateFromString: @"2018-02-18"];
        _datePicker1.textFontOfSelectedRow = TGAutoFont(12);
        _datePicker1.textFontOfOtherRow = TGAutoFont(12);
        _datePicker1.rowHeight = RatioHeight(31);
        _datePicker1.textColorOfOtherRow = UIColor.blackColor;// RGBA(195, 195, 195, 1);
        _datePicker1.datePickerMode = PGDatePickerModeDate;
        _datePicker1.delegate = self;
        _datePicker1.isHiddenMiddleText = YES;
        _datePicker1.textColorOfSelectedRow = RGBA(253, 119, 0, 1);
        _datePicker1.lineBackgroundColor = RGBA(253, 119, 0, 1);
    }
   
    return _datePicker1;
}

#pragma mark - Delegates

- (void)datePicker:(PGDatePicker *)datePicker didSelectDate:(NSDateComponents *)dateComponents {
    NSLog(@"dateComponents = %@", dateComponents);
    NSString * strTime = [NSString stringWithFormat:@"%d-%.2d-%.2d",(int)dateComponents.year,(int)dateComponents.month,(int)dateComponents.day];

    if (self.curentBtn.tag == 0) {
        self.startTime = strTime;
        [self.startButton setTitle:strTime forState:0];
    }else{
        self.endTime = strTime;
        [self.endButton setTitle:strTime forState:0];
    }
    if (self.startTime && self.endTime) {
        self.popView.sureBtnEnable = YES;
        if (self.selectTime) {
            self.selectTime(self.startTime, self.endTime);
        }        
    }
}
- (void)buttonAction:(LDButtonLine *)btn{
    
    if (_curentBtn && _curentBtn.tag == btn.tag) {
        return;
    }
    
    btn.selected = YES;
    _curentBtn.selected = NO;
    _curentBtn = btn;
    if (![_curentBtn.titleLabel.text containsString:@"选择"]) {
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
        dateFormatter.dateFormat = @"yyyy-MM-dd";
        [self.datePicker1 setDate:[dateFormatter dateFromString: _curentBtn.titleLabel.text] animated:YES];
    }
}

- (LDButtonLine *)startButton{
    if (!_startButton) {
        _startButton = [[LDButtonLine alloc] initWithFrame:CGRectMake(0, 0, DDBtnWidth, RatioHeight(32))];
        [_startButton setTitleColor:RGBA(253, 119, 0, 1) forState:UIControlStateSelected];
        [_startButton setTitleColor:RGBA(153, 153, 153, 1) forState:0];
        _startButton.titleLabel.font = TGAutoFont(13);
        [_startButton setTitle:@"选择开始日期" forState:0];
        [_startButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        _startButton.tag= 0;
    }
    return _startButton;
}

- (UIButton *)endButton{
    if (!_endButton) {
        _endButton = [[LDButtonLine alloc] initWithFrame:CGRectMake(0, 0, DDBtnWidth, RatioHeight(32))];
        _endButton.blueModel = self.isBlueModel;
        [_endButton setTitleColor:RGBA(153, 153, 153, 1) forState:0];
        _endButton.titleLabel.font = TGAutoFont(13);
        [_endButton setTitle:@"选择结束日期" forState:0];
        [_endButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        _endButton.tag = 1;
        [_endButton setTitleColor:RGBA(253, 119, 0, 1) forState:UIControlStateSelected];
       
    }
    return _endButton;
}
@end
